﻿
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
using System.Text;
using System.Xml;
using Gift.WebApp;
using Gift.DAL;
using Gift.Model;

namespace WebApp.admin.basic
{
    public partial class customNavigationList : BaseAdmin
    {
        // Fields
      
        protected string order = "id";
        protected int ot = 1;
        protected int pageCount = 0;
        protected int pageIndex = 1;
        protected int pageSize = 20;
        protected PageList pl = new PageList();
        protected DALPageList plm = new DALPageList();
        protected int total = 0;

        // Methods
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            string str = base.Request.Form["Item"];
            if (str.Contains(","))
            {
                Array array = str.Split(new char[] { ',' });
                foreach (string str2 in array)
                {
                    BaseFactory.theCustomNavigation.Delete(Convert.ToInt32(str2));
                }
            }
            else
            {
                BaseFactory.theCustomNavigation.Delete(Convert.ToInt32(str));
            }
            base.Response.Redirect(HttpContext.Current.Request.Url.PathAndQuery);
        }

        private void CustomNavigationBind()
        {
            try
            {
                if (base.Request.QueryString["p"] != null)
                {
                    this.pageIndex = int.Parse(base.Request.QueryString["p"]);
                }
                if (base.Request.QueryString["ps"] != null)
                {
                    this.pageSize = Convert.ToInt32(base.Request.QueryString["ps"]);
                }
                if (base.Request.QueryString["or"] != null)
                {
                    this.order = base.Request.QueryString["or"];
                }
                if (base.Request.QueryString["ot"] != null)
                {
                    this.ot = Convert.ToInt32(base.Request.QueryString["ot"]);
                }
                this.pl.TblName = "tbCustomNavigation";
                this.pl.StrGetFields = "id,title,url,sortOrder,Convert(nvarchar(10),createTime,121) as createTime";
                this.pl.FldName = this.order;
                this.pl.PageSize = this.pageSize;
                this.pl.PageIndex = this.pageIndex;
                this.pl.DoCount = 1;
                this.pl.OrderType = this.ot;
   
                this.total = Convert.ToInt32(this.plm.GetPageList(this.pl).Rows[0]["Total"]);
                this.pl.DoCount = 0;
                DataTable pageList = this.plm.GetPageList(this.pl);
                new PageListNo().DelPageSize(pageList, this.total);
                if (pageList.Rows.Count == 0)
                {
                    this.btnDelete.Visible = false;
                }
                this.rptNavigation.DataSource = pageList.DefaultView;
                this.rptNavigation.DataBind();
            }
            catch
            {
            }
        }

        protected override object LoadPageStateFromPersistenceMedium()
        {
            byte[] inArray = ViewStateCompression.DeCompress(Convert.FromBase64String(base.Request.Form.Get("__MSPVSTATE")));
            LosFormatter formatter = new LosFormatter();
            return formatter.Deserialize(Convert.ToBase64String(inArray));
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            //if ((HttpContext.Current.Session["admin_user"] == null) && (HttpContext.Current.Session["language_version"] == null))
            //{
            //    base.ClientScript.RegisterStartupScript(base.GetType(), "sJs", "top.location=\"login.html\";", true);
            //}
            if (!base.IsPostBack)
            {
                this.DeleItem();
                this.btnDelete.Attributes.Add("disabled", "disabled");
                this.CustomNavigationBind();
            }
        }

        private void DeleItem()
        {
            if (Request.QueryString["ot"] == "del")
            {
                int id = Convert.ToInt32(Request.QueryString["id"]);

                if (BaseFactory.theCustomNavigation.Delete(id))
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "sd", "<script language=\"javascript\" defer>alert('删除成功！');location.href='customNavigationList.aspx';</script>");
                }
                else
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "sd", "<script language=\"javascript\" defer>alert('删除失败！');location.href='customNavigationList.aspx';</script>");
                }
            }

        }

        protected override void SavePageStateToPersistenceMedium(object pViewState)
        {
            LosFormatter formatter = new LosFormatter();
            StringWriter output = new StringWriter();
            formatter.Serialize(output, pViewState);
            string hiddenFieldInitialValue = Convert.ToBase64String(ViewStateCompression.Compress(Convert.FromBase64String(output.ToString())));
            base.ClientScript.RegisterHiddenField("__MSPVSTATE", hiddenFieldInitialValue);
        }
    }
}
